package com.groupseven.backend.controller;

import com.groupseven.backend.domain.Log.OperationLog;
import com.groupseven.backend.domain.Log.SysLog;
import com.groupseven.backend.domain.ResultType;
import com.groupseven.backend.domain.Serch_data;
import com.groupseven.backend.services.OperationLogService;
import com.groupseven.backend.services.SysLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/v1/Log")
public class LogController {
    @Autowired
    private OperationLogService operationLogService;
    @Autowired
    private SysLogService sysLogService;

    @GetMapping("/OperationLog/")
    public ResultType selectOperation(){
        List<OperationLog> tmp =operationLogService.selectOperationLog();
        ResultType resultType=new ResultType(1,"查询成功",200);
        resultType.setOperationLogs(tmp);
        return resultType;
    }

    @GetMapping("/OperationLog/ways1/{username}")
    public ResultType selectOperationByusername(@PathVariable String username){
        if(operationLogService.selectOperationByusername(username).isEmpty()){
            return new  ResultType(0,"查询失败,用户名错误",200);
        }
        else {
            List<OperationLog> tmp=operationLogService.selectOperationByusername(username);
            ResultType resultType=new ResultType(1,"查询成功",200);
            resultType.setOperationLogs(tmp);
            return resultType;
        }
    }

    @GetMapping("/OperationLog/ways2/{createtime}")//输入年月日，查询当天
    public ResultType selectOperationBycreatetime(@PathVariable String createtime){
        if(operationLogService.selectOperationBycreatetime(createtime).isEmpty()){
            return new  ResultType(0,"查询失败,日期错误",200);
        }
        else {
            List<OperationLog> tmp=operationLogService.selectOperationBycreatetime(createtime);
            ResultType resultType=new ResultType(1,"查询成功",200);
            resultType.setOperationLogs(tmp);
            return resultType;
        }
    }


    /////////////////////////////////////////////////////////
    @DeleteMapping("/Operationlog/{createtime}")//输入单条记录中精确到秒的时间来精准删除
    public ResultType deleteOperationLogBycreatetime(@PathVariable String createtime){
        int a1=operationLogService.deleteOperationLogBycreatetime(createtime);
        if(a1==0){
            return new  ResultType(0,"删除失败,日期错误",200);
        }
        else {
            return new  ResultType(1,"删除成功",200);
        }
    }

    @DeleteMapping("/Systemlog/{createtime}")//输入单条记录中精确到秒的时间来精准删除
    public ResultType deleteSysLogBycreatetime(@PathVariable String createtime){
        int a1=sysLogService.deleteSysLogBycreatetime(createtime);
        if(a1==0){
            return new  ResultType(0,"删除失败,日期错误",200);
        }
        else {
            return new  ResultType(1,"删除成功",200);
        }
    }
    /////////////////////////////////////////////////
/////////////////////////////////////////////////
    @PostMapping("/OperationLog/multidel/")
    public ResultType mutildelBycreatetimelist(@RequestBody List<OperationLog> OperationLogs){//单条记录精确到秒的时间的数组
        List<Date> createtimes=new ArrayList<>();

        for(OperationLog operationLog:OperationLogs){

            createtimes.add(operationLog.createDate);
        }
        int a1=operationLogService.mutildelBycreatetimelist(createtimes);
        if(a1==0){
            return new  ResultType(0,"删除失败,日期错误",200);
        }
        else {
            return new  ResultType(1,"删除成功",200);
        }
    }
///////////////////////////////////////////////////



    @DeleteMapping(value = "/OperationLog/deleteallOperation/{username}")
    public ResultType deleteOperationLog(@PathVariable String username) {
        int a1= operationLogService.deleteOperationLog(username);
        if(a1==1){
            return new  ResultType(1,"删除成功",200);
        }
        else {
            return new ResultType(0,"删除失败,用户名不存在",200);
        }
    }
    @GetMapping("/SysLog/")
    public ResultType selectSysLog(){
        List<SysLog> tmp =sysLogService.selectSysLog();
        ResultType resultType=new ResultType(1,"查询成功",200);
        resultType.setSysLogs(tmp);
        return resultType;
    }


    //////////////////////////////////////////////////////
    @PostMapping(value = "/SysLog/multidel/")
    public ResultType mutildelSysLogBycreatetimelist(@RequestBody List<SysLog> SysLogs){//单条记录精确到秒的时间的数组
        List<Date> createtimes=new ArrayList<>();
        for(SysLog sysLog:SysLogs){
            createtimes.add(sysLog.createtime);
        }
        int a1=sysLogService.mutildelSysLogBycreatetimelist(createtimes);
        if(a1==0){
            return new  ResultType(0,"删除失败,日期错误",200);
        }
        else {
            return new  ResultType(1,"删除成功",200);
        }
    }
/////////////////////////////////////////////

    @PostMapping("/OperationLog/ways3/")//输入年月日，查询当天
    public ResultType selectOperationBystimeandetime(@RequestBody Serch_data serch_data){
        Date stime=serch_data.getStarttime();
        Date etime=serch_data.getEndtime();
        if(operationLogService.selectOperationBystimeandetime(stime,etime).isEmpty()){
            return new  ResultType(0,"查询失败,无数据",200);
        }
        else {
            List<OperationLog> operationLogs=operationLogService.selectOperationBystimeandetime(stime,etime);
            ResultType resultType=new ResultType(1,"查询成功",200);
            resultType.setOperationLogs(operationLogs);
            return resultType;
        }
    }

    @PostMapping("/SysLog/ways3/")//输入年月日，查询当天
    public ResultType selectSysLogBystimeandetime(@RequestBody Serch_data serch_data){
        Date stime=serch_data.getStarttime();
        Date etime=serch_data.getEndtime();
        if(sysLogService.selectSysLogByBystimeandetime(stime,etime).isEmpty()){
            return new  ResultType(0,"查询失败,无数据",200);
        }
        else {
            List<SysLog> sysLogs=sysLogService.selectSysLogByBystimeandetime(stime,etime);
            ResultType resultType=new ResultType(1,"查询成功",200);
            resultType.setSysLogs(sysLogs);
            return resultType;
        }
    }
}
